﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Squadron.Core;
using System.Windows.Forms;
using Squadron.CanvasData;
using System.Data;
using Squadron.ExportData;

namespace Squadron
{
    public static class SquadronContext
    {
        private static CanvasDataForm _CanvasDataForm;

        internal static CanvasDataForm CanvasDataForm
        {
            get
            {
                if (_CanvasDataForm == null)
                    _CanvasDataForm = new CanvasDataForm();

                return _CanvasDataForm;
            }
        }

        private static ExportDataForm _ExportDataForm;

        internal static ExportDataForm ExportDataForm
        {
            get
            {
                if (_ExportDataForm == null)
                    _ExportDataForm = new ExportDataForm();

                return _ExportDataForm;
            }
        }

        private static SquadronUtils _SquadronUtils;

        public static SquadronUtils Utils
        {
            get
            {
                if (_SquadronUtils == null)
                    _SquadronUtils = new SquadronUtils();

                return _SquadronUtils;
            }
        }

        internal static MainForm MainForm { get; set; }

        private static AddinManager _AddinManager;

        public static SquadronAddin ActiveAddin { get; set; }

        public static AddinManager AddinManager
        {
            get
            {
                if (_AddinManager == null)
                {
                    _AddinManager = new AddinManager();
                }

                return _AddinManager;
            }
        }

        public static string Url
        {
            get
            {
                return MainForm.UrlText.Text;
            }
        }

        public static IList<SquadronAddin> Addins
        {
            get
            {
                return AddinManager.AddIns;
            }
        }

        public static void WriteMessage(string message)
        {
            string context = string.Empty;
            if (ActiveAddin != null)
                context +=  DateTime.Now.ToLongTimeString() + " [" + ActiveAddin.Name + "] ";

            MainForm.LogBox.Items.Add(context + message);
            MainForm.LogBox.SelectedIndex = MainForm.LogBox.Items.Count - 1;

            Application.DoEvents();
        }

        public static System.Data.DataTable GetCanvasDataTable()
        {
            if (SquadronContext.HasCanvasData(false))
            {
                DataTable table = new DataTable();

                foreach (object obj in SquadronContext.CanvasDataForm.SelectedColumns.CheckedItems)
                {
                    table.Columns.Add(obj.ToString(), typeof(string));
                }

                foreach (DataRow r in (SquadronContext.CanvasDataForm.DataGrid.DataSource as DataTable).Rows)
                {
                    DataRow row = table.NewRow();

                    foreach (DataColumn c in table.Columns)
                    {
                        row[c.ColumnName] = r[c.ColumnName];
                    }

                    table.Rows.Add(row);
                }

                return table;
            }

            return null;
        }

        public static bool HasCanvasData(bool force)
        {
            if (!HasCanvasData())
                if (force)
                {
                    SquadronContext.MainForm.ShowCanvasDataForm();
                }

            return HasCanvasData();
        }

        public static bool HasCanvasData()
        {
            bool result = (SquadronContext.CanvasDataForm.DataGrid != null)
                               && SquadronContext.CanvasDataForm.SelectedColumns.CheckedItems.Count > 0;

            return result;
        }
    }
}
